
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int DHT22 = 13;

unsigned char  Sensor_Data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char Sensor_Check;      //校验和

unsigned char Sensor_AnswerFlag;  //收到起始标志位
unsigned char Sensor_ErrorFlag;   //读取传感器错误标志
unsigned char Sys_CNT;
unsigned char  Tmp;
unsigned char  *String;
unsigned int count = 0;


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.

  unsigned int humidity;
  unsigned int tempterature;
   char buff[20];



  
  //pinMode(DHT22, OUTPUT);
  //pinMode(DHT22, INPUT);
  Serial.begin(115200);

   delay(2000);
    Read_Sensor();
    humidity =  Sensor_Data[0] << 8 | Sensor_Data[1];
    tempterature =  Sensor_Data[2] << 8 | Sensor_Data[3];

    sprintf(buff, "%03d,%03d\n", tempterature, humidity);
    //Serial.println(th);
    // Serial.println(tp);
    Serial.print(buff);

}

// the loop function runs over and over again forever
void loop() {
  unsigned int humidity;
  unsigned int tempterature;
  char buff[20];
  if (count++ > 1800)
  {
    Read_Sensor();
    humidity =  Sensor_Data[0] << 8 | Sensor_Data[1];
    tempterature =  Sensor_Data[2] << 8 | Sensor_Data[3];

    sprintf(buff, "%03d,%03d\n", tempterature, humidity);
    //Serial.println(th);
    // Serial.println(tp);
    Serial.print(buff);
    count = 0;
  }
  delay(1000);


  /*
    digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);                       // wait for a second
  */

}



/**
    read data from DHT22
*/
unsigned char  Read_SensorData (void)       //返回0,总线上DS18B20
{
  unsigned char i, cnt;
  unsigned char buffer, tmp;

  pinMode(DHT22, INPUT);
  buffer = 0;

  for (i = 0; i < 8; i++)
  {
    cnt = 0;
    while (!digitalRead(DHT22)) //检测上次低电平是否结束
    {
      if (++cnt >= 300)
      {
        break;
      }
    }
    delayMicroseconds(30);//延时30us

    //判断传感器发送数据位
    tmp = 0;
    if (digitalRead(DHT22))
    {
      tmp = 1;
    }
    cnt = 0;
    while (digitalRead(DHT22))  //等待高电平 结束
    {
      if (++cnt >= 200)
      {
        break;
      }
    }
    buffer <<= 1;
    buffer |= tmp;
  }
  return buffer;


}

//---------------------------------------------------------------------------------
/********************************************\
  |* 功能： 读传感器                        *|
  \********************************************/
unsigned char Read_Sensor(void)
{
  unsigned char i; //主机拉低(Min=800US Max=20Ms)
  pinMode(DHT22, OUTPUT);
  //P21_DOUT = 0;
  digitalWrite(DHT22, LOW);
  delay(2);//延时2Ms    //释放总线 延时(Min=30us Max=50us)
  //P21_DOUT = 1;
  digitalWrite(DHT22, HIGH);
  delayMicroseconds(30);//延时30us
  //主机设为输入 判断传感器响应信号

  //P21_DOUT = 1;
  pinMode(DHT22, INPUT);
  Sensor_AnswerFlag = 0;  // 传感器响应标志

  //判断从机是否有低电平响应信号 如不响应则跳出，响应则向下运行
  if (digitalRead(DHT22) == 0)
  {
    Sensor_AnswerFlag = 1;//收到起始信号
    Sys_CNT = 0;
    //判断从机是否发出 80us 的低电平响应信号是否结束
    while ((!digitalRead(DHT22)))
    {
      if (++Sys_CNT > 300) //防止进入死循环
      {
        Sensor_ErrorFlag = 1;
        return 0;
      }
    }
    Sys_CNT = 0;
    //判断从机是否发出 80us 的高电平，如发出则进入数据接收状态
    while ((digitalRead(DHT22)))
    {
      if (++Sys_CNT > 300) //防止进入死循环
      {
        Sensor_ErrorFlag = 1;
        return 0;
      }
    }
    // 数据接收 传感器共发送40位数据
    // 即5个字节 高位先送  5个字节分别为湿度高位 湿度低位 温度高位 温度低位 校验和
    // 校验和为：湿度高位+湿度低位+温度高位+温度低位
    for (i = 0; i < 5; i++)
    {
      Sensor_Data[i] = Read_SensorData();
    }
  }
  else
  {
    Sensor_AnswerFlag = 0;    // 未收到传感器响应
  }
  return 1;
}



